package com.appheader.rongmaphone.views;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.PopupWindow;

import com.appheader.rongmaphone.R;

/**
 * Created by zhep on 2016/9/20.
 */
public class MyPopWindow extends PopupWindow {

    private final View popupWindow_view;
    private View.OnKeyListener onKeyListener;
    private RecyclerView image1;
    private RecyclerView image2;


    public MyPopWindow(Context context){
        popupWindow_view = LayoutInflater.from(context).inflate(R.layout.pop_test, null, false);
        setContentView(popupWindow_view);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        onKeyListener = new View.OnKeyListener() {

            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                Log.d("hzb", "MediaControlerPopupWindow  --- OnKeyListener --- "+keyCode);

                if(keyCode == KeyEvent.KEYCODE_BACK){
                    dismiss();
                }
                return true;
            }
        };
////         点击其他地方消失
//        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//                if (mPopupWindow != null && mPopupWindow.isShowing()) {
//                    mPopupWindow.dismiss();
//                    mPopupWindow = null;
//                }
//                return false;
//            }
//        });
        popupWindow_view.setOnKeyListener(onKeyListener);
        popupWindow_view.setFocusable(true);
        popupWindow_view.setFocusableInTouchMode(true);
        setFocusable(true);


        CheckBox switchOffOn = (CheckBox) popupWindow_view.findViewById(R.id.switch_off_on);
        image1 = (RecyclerView) popupWindow_view.findViewById(R.id.scroll_first);
        image2 = (RecyclerView) popupWindow_view.findViewById(R.id.scroll_second);
        image1.setVisibility(View.VISIBLE);
        image2.setVisibility(View.GONE);

        switchOffOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (image1.getVisibility()==View.VISIBLE){
                    image1.setVisibility(View.GONE);
                    image2.setVisibility(View.VISIBLE);
                }else {
                    image1.setVisibility(View.VISIBLE);
                    image2.setVisibility(View.GONE);
                }

            }
        });
    }
}
